package jrdcom.com.providertest;

import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView listview;
    private List<String> dataList = new ArrayList<String>();
    private ArrayAdapter arrayAdapter;
    private final static int MY_READ_REQUEST = 1;
    private final static int MY_WRITE_REQUEST = 2;
    private final static String TAG = "zlwu";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);

        //获取控件元素
        Button btn_insert = (Button)findViewById(R.id.btn_insert);
        Button btn_query = (Button)findViewById(R.id.btn_query);
        btn_insert.setOnClickListener(this);
        btn_query.setOnClickListener(this);

        listview = (ListView)findViewById(R.id.list_view);
        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList);
        listview.setAdapter(arrayAdapter);
    }

    private void insertData(){
        Uri uri = Uri.parse("content://jrdcom.com.contentprovidertest.provider/stu");
        ContentValues contentValues =  new ContentValues();
        contentValues.put("stu_name", "wuzhaolin");
        Uri new_uri = getContentResolver().insert(uri, contentValues);
        String id = new_uri.getPathSegments().get(1);
    }

    private void quertData(){
        Uri uri_query = Uri.parse("content://jrdcom.com.contentprovidertest.provider/stu");
        Cursor cursor = null;
        try{
            cursor = getContentResolver().query(uri_query, null,null, null,null,null);
            if(cursor.moveToFirst()){
                while (cursor.moveToNext()){
                    String stu_name = cursor.getString(cursor.getColumnIndex("stu_name"));
                    Log.d(TAG, "queryData, stu_name = "+stu_name);
                    dataList.add(stu_name);
                }
            }
            arrayAdapter.notifyDataSetChanged();
        }catch (Exception e){

        }finally {
            cursor.close();
        }
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_insert:
                //if(ContextCompat.checkSelfPermission(this,"jrdcom.com.contentProvider.write") == PackageManager.PERMISSION_DENIED){
                    //获取权限
                //    ActivityCompat.requestPermissions(this, new String[]{"jrdcom.com.contentProvider.write"},MY_WRITE_REQUEST);
                //}else{
                    insertData();
                //}
                break;
            case R.id.btn_query:
                //if(ContextCompat.checkSelfPermission(this,"jrdcom.com.contentProvider.read") == PackageManager.PERMISSION_DENIED){
                //获取权限
                //    ActivityCompat.requestPermissions(this, new String[]{"jrdcom.com.contentProvider.read"},MY_READ_REQUEST);
                //}else{
                    quertData();
                //}
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode){
            case MY_READ_REQUEST:
                quertData();
                break;
            case MY_WRITE_REQUEST:
                if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    insertData();
                }
                break;
        }
    }
}
